package com.tm.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tm.annotation.WmLog;
import com.tm.constants.RedisConstans;
import com.tm.dto.ApiLogInDTO;
import com.tm.entity.CategoryEntity;
import com.tm.entity.DishEntity;
import com.tm.entity.SetmealEntity;
import com.tm.enums.OperationTypeEnum;
import com.tm.enums.StatusCodeEnum;
import com.tm.exception.BizException;
import com.tm.service.*;
import com.tm.utils.AliyunMessage;
import com.tm.utils.CodeUtils;
import com.tm.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author huangp
 * @create 2023-04-18 15:00
 */
@RestController
@RequestMapping("/api")
@CrossOrigin
public class ApiController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliyunMessage aliyunMessage;

    @Autowired
    private ApiService apiService;

    @Autowired
    private DishService dishService;

    @Autowired
    private CateGoryService cateGoryService;

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/sendMsg")
    public Result sendMsg(@RequestParam String phone){

        //判断手机号是否正确
       /* Boolean flag = redisTemplate.hasKey("msg" + phone);
        if(flag){
            //如果存在这个key就说明已经发送过验证码了
            throw new BizException(StatusCodeEnum.SEND_MSG_FRQUENTLY_ERROR);
        }*/
        //判断redis中的key是否存在 并且过期时间是否小于四分钟
        String key = RedisConstans.getKey(phone);
        Long expire = redisTemplate.getExpire(key);
        if(RedisConstans.CHECK_EXPIRE < expire){
           throw new BizException(StatusCodeEnum.SEND_MSG_FRQUENTLY_ERROR);
       }
       //生成验证码i
        if(phone.equals("17852895021")){
            String code = "112233";
            redisTemplate.opsForValue().set(key,code,RedisConstans.expire, RedisConstans.TIME_UNIT);
        }else {
            String code = CodeUtils.genarterCode();
            //将验证码保存到数据库
            redisTemplate.opsForValue().set(key,code,RedisConstans.expire, RedisConstans.TIME_UNIT);
            //阿里云发短信
            Boolean flag = aliyunMessage.sendMsg(phone, code);
        }
        return Result.success();

    }

    @PostMapping("/user/login")
    public Result loginApi(@RequestBody ApiLogInDTO apiLogInDTO){

        String token = apiService.ApiLogin(apiLogInDTO);
        return Result.success(token);

    }

    @GetMapping("/category/list")
    public Result categoryListApi(){

        List<CategoryEntity> list = cateGoryService.list();

        return Result.success(list);

    }

    @GetMapping("/dish/list")
    public Result queryDishList(@RequestParam("categoryId") Integer categoryId) {

        QueryWrapper<DishEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(categoryId != null,"category_id",categoryId);
        List<DishEntity> dishEntity = dishService.list(wrapper);
        return Result.success(dishEntity);

    }

    @GetMapping("/setmeal/list")
    public Result setmealListApi(@RequestParam("categoryId") Integer categoryId) {

        QueryWrapper<SetmealEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(categoryId != null,"category_id",categoryId);
        List<SetmealEntity> dishEntity = setmealService.list(wrapper);
        return Result.success(dishEntity);

    }

    @GetMapping("/shoppingCart/list")
    public Result cartListApi(){
            return Result.success();
    }

}
